-module(algo).

-export([qsort/1,toheap/1,fib/1]).

qsort([]) ->
    [];
qsort([H|Rest]) ->
    qsort([ N || N<-Rest, N<H]) ++ [H] ++ qsort([ N || N<-Rest, N>H]).


%because erlang don't support array, so cann't implement heap
toheap(List) ->
    heaping(List,[]).

heaping([H|Rest],Heap) ->
    NewHeap = Heap ++ [H],
    heaping(Rest,NewHeap);
heaping([],Heap) ->
    Heap.

fib(N) when is_number(N) ->
    fib_a(0,1,N);
fib(_Arg) ->
    {error,errorDataType}.

%%从上到下的匹配
fib_a(F1,_F2,1) ->
    F1;	
fib_a(F1,F2,Num) ->
    fib_a(F2,F1+F2,Num-1).
 
